# models.py
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
import hashlib
from datetime import datetime

db = SQLAlchemy()

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), unique=True, nullable=False)
    email = db.Column(db.String(150), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False) 
    phone = db.Column(db.String(20))
    
    def set_password(self, password):
        """设置密码 - 使用明文存储（与数据库一致）"""
        self.password = password
    
    def check_password(self, password):
        """检查密码 - 直接比较明文"""
        return self.password == password

class Movie(db.Model):
    __tablename__ = 'movies'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    director = db.Column(db.String(150))
    actors = db.Column(db.String(300))
    description = db.Column(db.Text)
    duration = db.Column(db.Integer)
    release_date = db.Column(db.Date)
    genre = db.Column(db.String(100))
    poster_url = db.Column(db.String(300), default='default.jpg')
    language = db.Column(db.String(100))
  
    
    # 定义与场次的一对多关系
    shows = db.relationship('Show', backref='movie', lazy=True)

class Cinema(db.Model):
    __tablename__ = 'cinemas'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(300), nullable=False)
    address = db.Column(db.String(300))
    city = db.Column(db.String(100))
    phone = db.Column(db.String(20))
    
    # 定义与场次的一对多关系
    shows = db.relationship('Show', backref='cinema', lazy=True)

class Show(db.Model):
    __tablename__ = 'shows'
    
    id = db.Column(db.Integer, primary_key=True)
    movie_id = db.Column(db.Integer, db.ForeignKey('movies.id'), nullable=False)
    cinema_id = db.Column(db.Integer, db.ForeignKey('cinemas.id'), nullable=False)
    start_time = db.Column(db.DateTime, nullable=False)
    end_time = db.Column(db.DateTime, nullable=False)
    hall = db.Column(db.String(100))
    price = db.Column(db.Numeric(10, 2), nullable=False)
    available_seats = db.Column(db.Integer, nullable=False)
    total_seats = db.Column(db.Integer, nullable=False)
    
    # 定义与订单的一对多关系
    orders = db.relationship('Order', backref='show', lazy=True)

class Order(db.Model):
    __tablename__ = 'orders'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    show_id = db.Column(db.Integer, db.ForeignKey('shows.id'), nullable=False)
    seats = db.Column(db.String(200), nullable=False)
    total_price = db.Column(db.Numeric(10, 2), nullable=False)
    status = db.Column(db.String(30), default='pending')
    payment_method = db.Column(db.String(100))
    transaction_id = db.Column(db.String(150))
    
    # 定义与用户的关系
    user = db.relationship('User', backref='orders')